<?php

require_once(WSB_MODULES_PATH .'sherpa/models/BaseGateway.php');
require_once(WSB_MODULES_PATH .'sherpa/models/Images.php');
require_once(WSB_MODULES_PATH .'sherpa/models/Image.php');

class Sherpa_Model_ImageGateway extends Sherpa_Model_BaseGateway
{

	/**
	 * Liefert nur die Hauptbilder der Elternprodukte.
	 *
	 * Ein Produkt kann mehrere Bilder (=Motive) enthalten. Es wird nur das
	 * Hauptbild (d.h. das erste aller Produktbilder) geliefert.
	 *
	 * @param array $parentProductIds
	 * @param integer $languageId
	 * @return array|Sherpa_Model_Images
	 */
	public function fetchParentProductMainImages(array $parentProductIds, $languageId)
	{
		$rows = array();
		if(!empty($parentProductIds)) {
			$db = $this->getDb();
			$sql = '
				SELECT *
				FROM sherpa_images AS pc
				WHERE languageId = ?
				AND parentProductId IN ( ' . implode(', ', $parentProductIds) . ' )
				ORDER BY parentProductId ASC, sort ASC
			';
			$stmt = $db->query($sql, array($languageId));
			$ids = array();
			foreach($stmt->fetchAll() AS $row) {
				$id = $row['parentProductId'];
				if(!in_array($id, $ids)) {
					$rows[] = $row;
					$ids[] = $id;
				}
			}
		}
		if($this->useModels()) {
			new Sherpa_Model_Images($rows);
		} else {
			return $rows;
		}
	}

	/**
	 * Liefert alle Bilder eines Elternprodukts.
	 *
	 * Ein Produkt kann mehrere Bilder (=Motive) enthalten. Es werden alle
	 * Bilder geliefert.
	 *
	 *
	 * @param integer $parentProductId
	 * @param integer $languageId
	 * @return array|Sherpa_Model_Images
	 */
	public function fetchParentProductImages($parentProductId, $languageId)
	{
		$select = $this->getDbSelect();
		$select->from(array('i' => 'sherpa_images'));
		$select->where('i.languageId = ?', $languageId);
		$select->where('i.parentProductId = ?', $parentProductId);
		//$select->order('parentProductId ASC');
		$select->order('sort ASC');
		$rows = $select->query()->fetchAll();
		if($this->useModels()) {
			new Sherpa_Model_Images($rows);
		} else {
			return $rows;
		}
	}

	/**
	 * @param array|integer $parentProductIds
	 * @return array|Sherpa_Model_Images
	 */
	public function fetchParentProductImageVariants($parentProductIds)
	{
		if(is_int($parentProductIds)) {
			$parentProductIds = array($parentProductIds);
		}
		$rows = array();
		if(!empty($parentProductIds)) {
			require_once(WSB_MODULES_PATH .'sherpa/models/ImageVariants.php');
			$select = $this->getDbSelect();
			$select->from(array('i' => 'sherpa_images'), array('parentProductId'));
			$select->join(array('itca' => 'sherpa_imageToColorApplication'), 'i.id = itca.imageId');
			$select->where('i.parentProductId IN ( ? )', $parentProductIds);
			$rows = $select->query()->fetchAll();
		}
		if($this->useModels()) {
			return new Sherpa_Model_ImageVariants($rows);
		} else {
			return $rows;
		}
	}

	/**
	 * @param integer $imageId
	 * @param integer $colorApplicationId
	 * @return array|Sherpa_Model_Images
	 */
	public function fetchImageVariant($imageId, $colorApplicationId)
	{
		require_once(WSB_MODULES_PATH .'sherpa/models/ImageVariant.php');

		$select = $this->getDbSelect();
		$select->from(array('i' => 'sherpa_images'), array('parentProductId'));
		$select->join(array('itca' => 'sherpa_imageToColorApplication'), 'i.id = itca.imageId');
		$select->where('i.id = ?', $imageId);
		$select->where('itca.colorApplicationId = ?', $colorApplicationId);
		$stmt = $this->getDb()->query($select);
		if($this->useModels()) {
			return new Sherpa_Model_ImageVariant($stmt->fetch());
		} else {
			return $stmt->fetch();
		}
	}

	/**
	 * @param integer $imageId
	 * @param integer $languageId
	 * @return array|Sherpa_Model_Image
	 */
	public function fetchImage($imageId, $languageId = 0)
	{
		$select = $this->getDbSelect();
		$select->from(array('i' => 'sherpa_images'));
		$select->where('i.id = ?', $imageId);
		if($languageId > 0) {
			$select->where('i.languageId = ?', $languageId);
		}
		$stmt = $this->getDb()->query($select);
		if($this->useModels()) {
			return new Sherpa_Model_Image($stmt->fetch());
		} else {
			return $stmt->fetch();
		}
	}

}
